import { createSlice } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit'
import type { RootState } from './index'
import { CardUpdatePriceInfo, SelectCardUpdatePriceParam } from '@/types/prepaidCard'
import { OrderCouponInfo } from '@/types/orderCoupon'

interface ConfirmPay {
  /**礼品卡更新价格 */
  cardUpdatePriceInfo: Partial<CardUpdatePriceInfo>
  /**优惠券 数据暂存 */
  orderCoupon: Partial<OrderCouponInfo>
  /** 确认更新参数缓存 */
  confirmUpdatePriceParam: Partial<SelectCardUpdatePriceParam>
}

const initialState: ConfirmPay = {
  cardUpdatePriceInfo: {},
  orderCoupon: {},
  confirmUpdatePriceParam: {},
}

export const confirmPaySlice = createSlice({
  name: 'counter',
  initialState,
  reducers: {
    /** 默认优惠券自动扣件*/
    couponChangePrice: (state, action: PayloadAction<CardUpdatePriceInfo>) => {
      state.cardUpdatePriceInfo = action.payload
    },
    confirmUpdatePrice: (state, action: PayloadAction<CardUpdatePriceInfo>) => {
      state.cardUpdatePriceInfo = action.payload
    },
    updateOrderCoupon: (state, action: PayloadAction<OrderCouponInfo>) => {
      state.orderCoupon = action.payload
    },
    changePriceParam: (state, action: PayloadAction<SelectCardUpdatePriceParam>) => {
      state.confirmUpdatePriceParam = { ...state.confirmUpdatePriceParam, ...action.payload }
    },
  },
})

export const { couponChangePrice, confirmUpdatePrice, updateOrderCoupon, changePriceParam } =
  confirmPaySlice.actions
export const selectCardUpdatePriceInfo = (state: RootState) => state.confirmPay.cardUpdatePriceInfo
export const selectOrderCoupon = (state: RootState) => state.confirmPay.orderCoupon
export const selectConfirmUpdatePriceParam = (state: RootState) =>
  state.confirmPay.confirmUpdatePriceParam
export default confirmPaySlice.reducer
